<template>
	<view class="login-container">
		<view class="login-form">
			<u--form labelPosition="left" :model="userInfo" :rules="rules" ref="uForm" :errorType="errorType">
				<u-form-item label="用户" prop="userName" borderBottom ref="item1">
					<u--input v-model="userInfo.userName" border="none" clearable placeholder="请输入用户名"
						prefixIcon="account-fill" prefixIconStyle="font-size: 22px;color: #909399"></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="password" borderBottom ref="item1">
					<u--input v-model="userInfo.password" type="password" border="none" clearable placeholder="请输入密码"
						prefixIcon="lock-fill" prefixIconStyle="font-size: 22px;color: #909399">
					</u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="login-submit">
			<view class="submit-button">
				<u-button type="success" text="登录" @click="login"></u-button>
			</view>
			<view class="submit-register">
				<u--text text="没有账号,请注册" color="red" align="right" @click="toRegister"></u--text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					userName: '',
					password: '',
				},
				rules: {
					'userName': {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
					'password': [{
							type: 'string',
							required: true,
							message: '请填写密码',
							trigger: ['blur', 'change']
						},
						// 6-8个字符之间的判断
						{
							min: 4,
							max: 8,
							message: '长度在4-8个字符之间',
							trigger: ['blur', 'change']
						},
					]
				},
				errorType: 'border-bottom',
			};
		},
		methods: {
			toRegister() {
				uni.navigateTo({
					url: '/pages/register/register'
				})
			},
			login() {

				var _this = this
				this.$refs.uForm.validate().then(res => {
					uni.$u.toast('校验通过')
					uni.request({
						url: 'http://127.0.0.1:8080/sysuser/login2',
						method: 'POST',
						data: _this.userInfo,
						success: (res) => {
							console.log(res.data);
							uni.setStorage({
								key: 'token',
								data: res.data.data,
								success: function() {
									uni.reLaunch({
										url:uni.getStorageSync("targetUrl")
									})
								}
							});

						},
						fail: (err) => {
							console.log('err', err)
						}
					});
					// uni.request({
					//     url: 'http://www.127.0.0.1:8080/sysuser/login2/' + userInfo.userName + '/' + userInfo.password , 
					// 	method:"POST",					  
					//     success: (res) => {
					//         console.log('res',res);
					//     },
					// 	fail: (error) => {
					// 		console.log('error',error);
					// 	}
					// });
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-container {
		.login-form {
			margin-top: 40px;
			padding: 10px;
		}

		.login-submit {
			padding: 10px;

			// display: flex;
			// flex-direction: row;
			// justify-content: space-around;
			.submit-register {
				margin-top: 5px;
			}
		}
	}
</style>
